package com.example.demo.config;


import com.alibaba.fastjson.support.spring.GenericFastJsonRedisSerializer;
import com.example.demo.entity.Channel;
import com.example.demo.repository.ChannelRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.listener.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;
import org.springframework.data.redis.serializer.GenericToStringSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;

import java.time.Duration;
import java.util.HashMap;
import java.util.Iterator;

@Configuration
public class RedisConfig {
    @Autowired
    public LettuceConnectionFactory connectionFactory;
    @Autowired
    private ChannelRepository channelRepository;

    @Bean
    RedisTemplate<String, Object> redisTemplate() {
        final RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(connectionFactory);
        GenericFastJsonRedisSerializer genericFastJsonRedisSerializer = new GenericFastJsonRedisSerializer();
        template.setKeySerializer(new GenericToStringSerializer<>(Object.class));
        template.setValueSerializer(genericFastJsonRedisSerializer);
        template.setHashKeySerializer(new GenericToStringSerializer<>(Object.class));
        template.setHashValueSerializer(genericFastJsonRedisSerializer);
        return template;
    }

    @Bean("cacheManager")
    public RedisCacheManager cacheManager(RedisConnectionFactory connectionFactory) {
        RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig().entryTtl(Duration.ofSeconds(60));
        HashMap<String, RedisCacheConfiguration> redisCacheConfigurations = new HashMap<>(1);
        redisCacheConfigurations.put("beidouDictionary", redisCacheConfiguration);
        return RedisCacheManager.RedisCacheManagerBuilder
                .fromConnectionFactory(connectionFactory)
                .withInitialCacheConfigurations(redisCacheConfigurations)
                .build();
    }

    @Bean
    RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory, MessageListenerAdapter listenerAdapter) {
        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        container.setConnectionFactory(connectionFactory);
        //订阅多个频道
        //组网信息
        Iterator<Channel> iterator = channelRepository.findAll().iterator();
        while (iterator.hasNext()){
            container.addMessageListener(listenerAdapter,new PatternTopic(iterator.next().getChannel()));
        }
//        container.addMessageListener(listenerAdapter, new PatternTopic("rover"));
//        container.addMessageListener(listenerAdapter, new PatternTopic("one"));
        //流动站信息
        //序列化对象
        container.setTopicSerializer(RedisSerializer.string());
        return container;
    }


    @Bean
    MessageListenerAdapter listenerAdapter(RedisMessageReceiver receiver) {
        return new MessageListenerAdapter(receiver, "onMessage");
    }
}
